<script setup>
	import { ref, reactive } from 'vue';
	import { uniFetch } from "../../../utils/fetch.js"
	import {useUserStore} from "../../../store/user.js"
	import  {onLoad} from "@dcloudio/uni-app"
	const userStore=useUserStore()
const rediurl=ref('');
const routetype=ref('');
	const form = reactive({
		account: "admin235",
		password: "123321"
	})
	const formm = ref();
	const rules = reactive({
		account: {
			rules: [{
					required: true,
					errorMessage: "请填写账号"
				},
				{
					pattern: '^[a-zA-Z0-9]{6,8}$',
					errorMessage: "登录账号格式不正确"
				}
			]
		},
		password: {
			rules: [{
					required: true,
					errorMessage: "请填写密码"
				},
				{
					pattern: '^\\d{6}$',
					errorMessage: "登录密码格式不正确"
				}
			]
		}

	})
	onLoad(e=>{
		console.log(e,"e");
		routetype.value=e.routeType
		rediurl.value=e.redirectURL

	})
	const submit = async () => {
		try {
			const form = await formm.value.validate();
			console.log(form);
			uniFetch({
				url: "/driver/login/account",
				method: "POST",
				data: form
			}).then(res => {
				console.log(res, "res");
				if(res.data.code !==200){
					return uni.utils.toast("登录失败")
				}else{
					console.log(userStore);
					userStore.token=res.data.data
					console.log(routetype.value,rediurl.value);
					// uni[routetype.value]({
					// 	url:rediurl.value
					// })
					if(routetype.value === 'switchTab'){
						uni.switchTab({
							url:'/'+rediurl.value
						})
					}else{
					uni.redirectTo({
						url:'/'+rediurl.value
					})						
					}

				}
			})
		} catch (e) {
			console.log(e);
			//TODO handle the exception
		}
	}
</script>

<template>
	<uni-forms :rules="rules" class="login-form" ref="formm" :model="form">
		<uni-forms-item name="account">
			<input type="text" v-model="form.account" placeholder="请输入账号" class="uni-input-input"
				placeholder-style="color: #818181" />
		</uni-forms-item>
		<uni-forms-item name="password">
			<input type="text" v-model="form.password" placeholder="请输入密码" class="uni-input-input"
				placeholder-style="color: #818181" />
		</uni-forms-item>
		<button @click="submit" class="submit-button">登录</button>
	</uni-forms>
</template>


<style lang="scss" scoped>
	@import './styles.scss';
</style>